
#include "loadingscene.h"
#include "resourcemanager.h"
#include "gameplayscene.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "CCSkeletonCache.h"

LoadingScene::LoadingScene(): _layer(nullptr) {

}

LoadingScene::~LoadingScene() {
}

bool LoadingScene::init() {
	if (!Scene::init()) {
		return false;
	}

	_layer = Layer::create();
	addChild(_layer);

	RM->createBackground(_layer, "textures/logo.png");

	srand(0);

	scheduleUpdate();

	return true;
}

void LoadingScene::load() {
	GDM->init();
	RM->init();

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("textures/units/units.plist");

	log("load skeletons");
	
	const char* jsons[] = {
		"enemy1",
		"enemy2",
		"swordsman",
		"archer",
		"alchemist",
		"miner",
	};

	const char* jsonFile = "json/%s.json";
	char buf[200];
	for (int i = 0; i < sizeof(jsons) / sizeof(char *); i++) {
		sprintf(buf, jsonFile, jsons[i]);
		CCSkeletonCache::getInstance()->loadSkeleton(jsons[i], buf);
	}

	log("loaded skeletons.");
}

void LoadingScene::update(float ticks) {
	load();

	Director::getInstance()->replaceScene(GamePlayScene::create());
}


